package com.LeeCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;

/**
 * 使数组的值全部为 K 的最少操作次数
 */

public class Code3375 {
    public static void main(String[] args) {
        int[] nums = {5, 2, 5, 4, 5, 1};
        System.out.println(new Code3375().minOperations(nums, 2));
    }

    public int minOperations(int[] nums, int k) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        ArrayList<Integer> list = new ArrayList<>(set);
        Collections.sort(list);

        if (list.get(0) < k) {
            return -1;
        }
        int i = list.size() - 1;
        for (; i >= 0; i--) {
            if (list.get(i) <= k) {
                break;
            }
        }
        return list.size() - 1 - i;
    }

    public int minOperations1(int[] nums, int k) {
        HashSet<Integer> set = new HashSet<>();
        int ans = 0;
        for (int num : nums) {
            if (!set.contains(num)) {
                set.add(num);
                if (num < k) {
                    return -1;
                } else if (num > k) {
                    ans++;
                }
            }
        }
        return ans;
    }
}
